package com.readpost.controller;

import com.readpost.doman.Result;
import com.readpost.doman.Entity.ArticleCategories;
import com.readpost.service.ArticleCategoriesService;
import com.readpost.utils.ThreadLocalUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/articleCategories")
@Validated
@Log4j2
public class ArticleCategoriesController {

    @Autowired
    ArticleCategoriesService articleCategoriesService;

    /**
     * 增加文章分类
     * @param category 文章分类信息
     * @return Result
     */
    @PostMapping("/addCategory")
    public Result<String> addCategory(@RequestBody @Validated(ArticleCategories.Add.class)  ArticleCategories category) {
        if (articleCategoriesService.isCategoryNameExists(category)){
            return Result.error(400,"分类名称已存在");
        }else {
            articleCategoriesService.addCategory(category);
            return Result.success("添加成功");
        }
    }


    /**
     * 获取所有文章分类
     * @return List 文章分类列表
     */
    @GetMapping("/getAllCategory")
    public Result<List<ArticleCategories>> getAllCategory() {
        List<ArticleCategories> categories = articleCategoriesService.getAllCategory();
        return Result.success(categories);
    }

    /**
     * 删除文章分类
     * @param categoryId 文章分类ID
     * @return Result 删除结果信息
     */
    @DeleteMapping("/deleteCategory/{categoryId}")
    public Result<String> deleteCategory(@Validated @PathVariable Integer categoryId) {
        if (articleCategoriesService.getCategoryById(categoryId) == null) {
            return Result.error(400, "分类不存在");
        }else {
            try {
                articleCategoriesService.deleteCategory(categoryId);
                return Result.success("删除成功");
            }
            catch (Exception e) {
                log.error("删除文章分类失败", e);
                return Result.error(500, "删除文章分类失败");
            }
        }

    }

    /**
     * 获取文章分类详情
     * @return Result 文章分类详情
     */
    @GetMapping("/detailCategory")
    public Result<ArticleCategories> detailCategory(@Validated Integer categoryId){
        ArticleCategories categories = articleCategoriesService.getDetailCategory(categoryId);
        return Result.success(categories);
    }


    /**
     * 修改文章分类
     * @param category 文章分类信息
     * @return Result 修改结果信息
     */
    @PutMapping("/updateCategory")
    public Result<String> updateCategory(@RequestBody @Validated(ArticleCategories.Update.class) ArticleCategories category) {
        if (articleCategoriesService.isCategoryIdExists(category.getCategoryId())){
//            Map<String,Object> map = ThreadLocalUtil.get();
//            String username = (String) map.get("username");
//            category.setModifiedUser(username);
            articleCategoriesService.updateCategory(category);
            return Result.success("修改成功");
        }else {
            return Result.error(400,"分类名称不存在");
        }
    }
}
